/*
* Copyright (C) 2021 ~ 2022 Uniontech Technology Co., Ltd.
*
* Author:      zhangbingyi <zhangbingyi@uniontech.com>
*
* Maintainer:  zhangbingyi <zhangbingyi@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "common/usbmanagercomm.h"

#include <QStyledItemDelegate>

// 白名单列表 各项列数
#define USB_NAME_COL 0 // 连接记录列表 名称项列数
#define USB_TIME_COL 1 // 连接记录列表 时间项列数
#define USB_STRATEGY_COL 2 // 连接记录列表 连接限制列数
#define USB_BTN_COL 3 // 连接记录列表 按钮项列数
#define USB_DATA_COL 0 // 连接记录列表 数据项列数 存放在名称列

// 数据存储偏移
#define DATA_ROLE_NAME Qt::UserRole + 1 // 数据存储偏移 - 名称
#define DATA_ROLE_TIME Qt::UserRole + 2 // 数据存储偏移 - 时间
#define DATA_ROLE_STRATEGY Qt::UserRole + 3 // 数据存储偏移 - 限制状态
#define DATA_ROLE_SERIAL Qt::UserRole + 4 // 数据存储偏移 - 序列号
#define DATA_ROLE_VENDOR_ID Qt::UserRole + 5 // 数据存储偏移 - 制造商id
#define DATA_ROLE_PRODUCT_ID Qt::UserRole + 6 // 数据存储偏移 - 生产商id
#define DATA_ROLE_OP_STATUS Qt::UserRole + 7 // 数据存储偏移 - 操作（添加/移除）白名单状态


class USBWhitelistDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    explicit USBWhitelistDelegate(QObject *parent = nullptr);
    virtual ~USBWhitelistDelegate() override;

    virtual void paint(QPainter *painter,
                       const QStyleOptionViewItem &option,
                       const QModelIndex &index) const override;

    virtual bool editorEvent(QEvent *event, QAbstractItemModel *model,
                             const QStyleOptionViewItem &option, const QModelIndex &index) override;

    // editing
    QWidget *createEditor(QWidget *parent,
                          const QStyleOptionViewItem &option,
                          const QModelIndex &index) const override;

    void setEditorData(QWidget *editor, const QModelIndex &index) const override;
    virtual void setModelData(QWidget *editor, QAbstractItemModel *model,
                              const QModelIndex &index) const override;

    void updateEditorGeometry(QWidget *editor,
                              const QStyleOptionViewItem &option,
                              const QModelIndex &index) const override;

protected:
    virtual bool helpEvent(QHelpEvent *event, QAbstractItemView *view,
                           const QStyleOptionViewItem &option, const QModelIndex &index) ;

public Q_SLOTS:
    // 当comboBox选项被激活
    void onComboBoxActived();

Q_SIGNALS:
    // 弹出详情页
    void notifyShowUSBDetails(USBConnectionInfo &currentinfo) const;
    // 操作白名单（添加、删除、修改）
    void notifyOperateWhitelist(int type, const USBConnectionInfo &currentinfo) const;
    void requestAuthorizeChangeDevPerm(const USBConnectionInfo &info) const;

private:
    USBConnectionInfo getInfoFromIndex(const QModelIndex &index) const;

private:
    bool m_isLightTheme;
    QPoint m_mousePoint;
    QRect m_viewDetails;
    QRect m_removeWhitelist;
};
